package com.example.goods_manage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.goods_manage.dto.Goods;
import com.example.goods_manage.dto.request.GoodsRequest;
import com.example.goods_manage.mapper.GoodsMapper;
import com.example.goods_manage.service.GoodsService;
import com.example.goods_manage.utils.AjaxResult;
import com.example.goods_manage.utils.PageUtils;
import com.sun.org.apache.xpath.internal.operations.Gt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/9/5:11:06
 */

@SuppressWarnings({"unchecked", ""})
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public AjaxResult findGoods(GoodsRequest goodsRequest) {
        //构造分页对象
        Page<Goods> page = new Page<>(goodsRequest.getPageNum(), goodsRequest.getPageSize());
        //List<Goods> goods = goodsMapper.findGoods(page, goodsRequest);
        List<Goods> goods = goodsMapper.findGoods(page, new HashMap<>());
        //设置查询的数据到 page对象中
        page.setRecords(goods);

        //分页的数据都会放到 page 对象中
        System.out.println(page.getCurrent());
        System.out.println(page.getTotal());
        System.out.println(page.getSize());
        PageUtils pageUtils = new PageUtils(page);
        return AjaxResult.success(pageUtils);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult addGoods(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        //checkName();
        //old
        //int i = goodsMapper.insertSelective(goods);
        //new
        int i = goodsMapper.insert(goods);
        if (i > 0) {
            return AjaxResult.success("新增成功");
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteGoods(GoodsRequest goodsRequest) {
        //xml
        //int i = goodsMapper.deleteByPrimaryKey(goodsRequest.getId());
        //plus 方法
        int i =  goodsMapper.deleteById(goodsRequest.getId());
        if (i > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败,可能不存在此条记录");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateGoods(GoodsRequest goodsRequest) {
        if (ObjectUtils.isEmpty(goodsRequest.getId())) {
            throw new NullPointerException("更新操作 主键id不能为空");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        //xml
        //int i = goodsMapper.updateByPrimaryKeySelective(goods);
        //plus
        int i =   goodsMapper.updateById(goods);
        if (i > 0) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.error("更新失败");
        }
    }
}
